qui(
    function (require, exports, module) {
        VODCom.Player = (function () {
            var playOnePosition = {
                left: '',
                top: ''
            }
            var flashvars = {};
            var textArr = [];
            var params = {};
            params.scale = "noscale";
            params.salign = "tl";
            params.wmode = "transparent";
            params.swliveconnect = "true";
            params.allowfullscreen = "true";
            params.allowscriptaccess = "always";
            params.allownetworking = "all";
            params.bgcolor = "#000000";
            var player = {};
            player.swf = "http://www.iqiyi.com/player/20130702143439/expressInstall.swf";
            player.version = "10.0.0";
            player.ScreenPrintUrl = "/epg/swfs/ScreenPrint.swf?20156291103";
            var settings = null;
            var _configOptions = null;
            var _accurateFrame = null;
            var _dao = null;
            var _tips = null;
            // 左侧播放器
            function initLeftPlayer() {
                VODCom.H5Player.buildPlayer("flash", "ppsLiveDiv");
                VODCom.H5Player.bindControl("flash");
                VODCom.H5Player.eventManager.bindEvent("need_append_mode", queryResource);
                VODCom.H5Player.eventManager.bindEvent("change_running_img", changeRunningImg);
            }

            function initViewImagePlayer() {
                VODCom.H5Player.buildPlayer("flashImage", "viewImageVideo");
            }

            // 单张图片视频预览播放器
            function initPicPlayer() {
                VODCom.H5Player.buildPlayer("flashOne", "ppsLiveOneFrame");
            }

// 起止帧精确化播放器
            function initFramePlayer() {
                var _attributes = {};
                _attributes.id = "accurateStartEndFrame";
                _attributes.pause = "暂停";
                params.scale = "Showall";
                if (checkFlash()) {
                    swfobject.embedSWF(player.ScreenPrintUrl, "accurateStartEndDiv", "100%", "100%", player.version, player.swf, flashvars, params, _attributes);
                } else {
                    noFlashTip("accurateStartEndDiv");
                }
            }

            //搭建js与flash互通的环境
            function thisMovie(movieName) {
                if (navigator.appName.indexOf("Microsoft") != -1) {
                    return window[movieName];
                } else {
                    return document[movieName];
                }
            }

            function playAll(allList, mode) {
                textArr = [];
                doViewImage(false);
                VODCom.H5Player.play("flash", allList.list)
            }

            function playOne(allList) {
                textArr = [];
                $.each(allList.list, function (i, element) {
                    textArr.push({url: element.url});
                })
                $("#play-one").css({
                    top: playOnePosition.top,
                    left: playOnePosition.left
                });
                $("#play-one").show();
                VODCom.H5Player.singlePlay('flashOne', textArr)
            }

            function initPlayStop(allList) {
                textArr = [];
                $.each(allList.list, function (i, element) {
                    textArr.push({url: element.url});
                })
                VODCom.H5Player.singlePlay('flashImage', textArr, true)
                if (VODCom.H5Player.getPlayerData("flash").video.paused) {
                    doViewImage(true);
                }
            }

            function doViewImage(show) {
                if (show) {
                    $("#video_preview_image_div").show();
                    $("#ppsLiveDiv").hide();
                    $("div.player_control").hide();
                } else {
                    $("#video_preview_image_div").hide();
                    $("#ppsLiveDiv").show();
                    $("div.player_control").show();
                }
            }

            function updatePosition(options) {
                playOnePosition.left = options.left;
                playOnePosition.top = options.top;
            }

            function initPlayer(configOptions) {
                _configOptions = configOptions
                _accurateFrame = getConfigEntity("accurateFrame");
                _dao = getConfigEntity("dao");
                settings = _dao.settings;
                _tips = getConfigEntity("tips");
                initLeftPlayer();
                initViewImagePlayer();
                initPicPlayer();
                initFramePlayer();
                registerCostTimeEvent();
            }

            function getConfigEntity(entityKey) {
                return _configOptions[entityKey];
            }

            function registerCostTimeEvent() {
                console.log("[Player]registerCostTimeEvent ");

                $("body").on("cost_time_event", function (event, data) {
                    EpgWeb.PingBackCost.doQuickSend({serverIp: _dao.getParamsData().ip, costTime: data.costTime + "", detail: {msg: data.isOverTime ? "time out" : "suc" }  })
                    if (data.isOverTime == true) {
                        SystemDialog.queryConfirm("图片请求失败，请退出重新加载", function (flag) {
                            _accurateFrame.accurateStartEndRemove();
                            if (flag) {
                                _accurateFrame.doLastHandlerQ();
                            }
                        }, {"btn_ok": "重新请求", "btn_cancel": "退出请求" });
                    }
                })
            }

            function queryResource(data) {
                var listUrl = _dao.getDotData().listUrl;
                $.get(listUrl, {
                    'key': _dao.settings.taskid,
                    'from_client': true,
                    'index': data.index,
                    'type': 0,
                    'offset': data.quickMode ? 2 : 30
                }, function (rdata) {
                    VODCom.H5Player.eventManager.dispatchEvent({type: "do_append", data: {playerId: data.playerId, resource: rdata }})
                }, 'json');
            }

            function changeRunningImg(data) {
                $("div img.fragment-img").css({backgroundColor: "transparent"})
                if (data.name != null) {
                    $("#img-" + data.name).css({backgroundColor: "green"})
                }
            }

            //播放功能
            function liveView(viewTime, options) {
                var duration = getFormatDuration(options.duration);
                doViewImage(false);
                VODCom.H5Player.startAppendMode("flash", {index: viewTime, url: options.url, duration: duration, name: options.name});
                //    var video VODCom.H5Player.getPlayerData("flash").video;
                //从刘志红那里取json列表
                //传给王鹏
            }

            //播放1帧
            function viewOne(viewTime) {
                //从刘志红那里取json列表
                var listUrl = _dao.getDotData().listUrl;
                $.get(listUrl, {
                    'key': _dao.settings.taskid,
                    'from_client': true,
                    'index': viewTime,
                    'type': 0,
                    'offset': 1
                }, function (data) {
                    playOne(data);
                }, 'json');
                //传给王鹏
            }

            //播放1帧
            function viewStopImage(viewTime) {
                var listUrl = _dao.getDotData().listUrl;
                $.get(listUrl, {
                    'key': _dao.settings.taskid,
                    'from_client': true,
                    'index': viewTime,
                    'type': 0,
                    'offset': 1
                }, function (data) {
                    initPlayStop(data);
                }, 'json');

            }

            function splitData(stime, etime, needRemoveDelData) {
                stime = parseInt(stime);
                etime = parseInt(etime);

                settings.subData = [];
                var needDatas = [];
                for (i = stime; i <= etime; i++) {
                    needDatas.push(i);
                }
                //console.log(needDatas);
                //console.log(delList);
                if (needRemoveDelData && settings.delList.length) {
                    $.each(settings.delList, function (k, v) {
                        v = parseInt(v);
                        if (stime <= v && v <= etime) {
                            for (var i = 0; i < needDatas.length; i++) { //删除v的元素
                                if (needDatas[i] == v) {
                                    needDatas.splice(i, 1);
                                    break;
                                }
                            }
                        }
                    })
                }
                settings.subData = needDatas;
                //console.log(subData);
            }

            function review() {
                if (!settings.subData.length) {
                    //alert('请先打点,再预览!');
                    _tips("error", '请先打点,再预览!');
                    return false;
                }
                var reviewData = {
                    'list': []
                };
                //console.log(subData);
                $.each(settings.subData, function (k, v) {
                    var src = $('#img-' + v).attr('src');
                    if (!src) {
                        //alert('数据为空不能预览!');
                        return false;
                    }
                    var duration = getFormatDuration($('#img-' + v).attr('duration'))
                    reviewData.list.push({url: src.replace(/jpg/g, "flv"), name: k + "", duration: duration  });
                })

                playAll(reviewData);
            }

            //打好的点预览
            var reviewOne = function (s, e, vType) {
                var reviewOneData = {
                    'list': []
                };
                s = parseInt(s);
                e = parseInt(e);
                var src , flv, obj, duration;
                for (var i = s; i <= e; i++) {
                    if (settings.delList.length && vType != "删除") {
                        var isNeed = true;
                        for (var j = 0; j <= settings.delList.length; j++) {
                            var v = settings.delList[j];
                            if (i == v) {
                                isNeed = false;
                            }
                        }
                        if (isNeed) {
                            src = $('#img-' + i).attr('src');
                            duration = getFormatDuration($('#img-' + v).attr('duration'))
                            obj = {url: src.replace(/jpg/g, "flv"), name: i + "", duration: duration }

                            reviewOneData.list.push(obj);
                        }
                    } else {
                        src = $('#img-' + i).attr('src');
                        duration = getFormatDuration($('#img-' + v).attr('duration'))
                        obj = {url: src.replace(/jpg/g, "flv"), name: i + "", duration: duration }
                        reviewOneData.list.push(obj);
                    }
                }
                //console.log(reviewOneData);
                playAll(reviewOneData);
            }

            function nofind(img_url) {
                var img = event.srcElement;
                img.src = img_url;
            }

            function checkFlash() {
                var flag = false;
                if (window.ActiveXObject) {
                    try {
                        var swf = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
                        if (swf) {
                            flag = true;
                        }
                    } catch (e) {
                    }
                } else {
                    try {
                        var swf = navigator.plugins['Shockwave Flash'];
                        if (swf) {
                            flag = true;
                        }
                    } catch (e) {
                    }
                }

                if (flag) {
                    console.log("flash running ok");
                } else {
                    console.log("flash running error");
                }

                return flag;
            }

            function noFlashTip(elementId) {
                var e = $("#" + elementId);
                if (!e) {
                    return;
                }
                e.html('<div class="position:absolute;top:50%;left:50%;z-index:99;margin-left:-165px;margin-top:-60px;padding:35px32px;width:266px;background:#eaedef;border-radius:5px;color:#333;"><p>您没有安装Flash播放器或者Flash播放器版本过低</p><p>请点击<a href="http://get.adobe.com/cn/flashplayer/">这里</a>安装</p><p class="grey">安装完毕之后请刷新页面或者重新启动浏览器</p></div>')
            }


            function getFormatDuration(duration) {
                if (duration == null || duration == "") {
                    return 2000;
                }
                return parseInt(duration);
            }

            function changePlayPause(playerId) {
                VODCom.H5Player.getPlayerData(playerId).control.playpause();
                if (!VODCom.H5Player.getPlayerData(playerId).video.paused) {
                    doViewImage(false);
                }
            }

            return {
                initPlayer: initPlayer,
                playAll: playAll,
                playOne: playOne,
                updatePosition: updatePosition,
                viewOne: viewOne,
                viewStopImage: viewStopImage,
                view: liveView,
                nofind: nofind,
                splitData: splitData,
                review: review,
                reviewOne: reviewOne,
                checkFlash: checkFlash,
                changePlayPause: changePlayPause,
                doViewImage: doViewImage
            }
        })()
    }
);